// workspace2.cpp : implementation of the CPropertyBar class
//

#include "stdafx.h"
#include "AnyCAD.h"
#include "PropertyBar.h"
#include "PropHolder.h"
#include "MessageGeomUI.h"
#include "mainfrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


const int nBorderSize = 1;

/////////////////////////////////////////////////////////////////////////////
// CPropertyBar

BEGIN_MESSAGE_MAP(CPropertyBar, CBCGPDockingControlBar)
	//{{AFX_MSG_MAP(CPropertyBar)
	ON_WM_CREATE()
	ON_WM_SIZE()
	//ON_WM_PAINT()
	//}}AFX_MSG_MAP
	ON_REGISTERED_MESSAGE(BCGM_PROPERTY_CHANGED,OnPropertiesChanged)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPropertyBar construction/destruction

CPropertyBar::CPropertyBar():m_pProp(NULL)
{
	// TODO: add one-time construction code here
}

CPropertyBar::~CPropertyBar()
{
}

/////////////////////////////////////////////////////////////////////////////
// CPropertyBar message handlers

int CPropertyBar::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CBCGPDockingControlBar::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	CRect rectDummy;
	rectDummy.SetRectEmpty ();
	
	if (!m_wndPropList.Create (WS_VISIBLE | WS_CHILD, rectDummy, this, 2))
	{
		TRACE0("Failed to create Properies Grid \n");
		return -1;      // fail to create
	}
	//InitPropList();
	m_wndPropList.EnableHeaderCtrl(false);
	m_wndPropList.EnableDescriptionArea(FALSE);
	m_wndPropList.SetVSDotNetLook(TRUE);
	//set prop background color
	m_wndPropList.SetCustomColors (
		RGB (228, 243, 254),
		RGB (46, 70, 165),
		RGB (200, 236, 209),
		RGB (33, 102, 49),
		RGB (255, 229, 216),
		RGB (128, 0, 0),
		RGB (159, 159, 255));
	return 0;
}

void CPropertyBar::OnSize(UINT nType, int cx, int cy) 
{
	CBCGPDockingControlBar::OnSize(nType, cx, cy);

	// Tab control should cover a whole client area:
	m_wndPropList.SetWindowPos (NULL, 0, 0, 
		cx, cy,
		SWP_NOACTIVATE | SWP_NOZORDER);
}
void CPropertyBar::SetPropListFont ()
{
	::DeleteObject (m_fntPropList.Detach ());

	LOGFONT lf;
	globalData.fontRegular.GetLogFont (&lf);

	NONCLIENTMETRICS info;
	info.cbSize = sizeof(info);

	::SystemParametersInfo (SPI_GETNONCLIENTMETRICS, sizeof(info), &info, 0);

	lf.lfHeight = info.lfMenuFont.lfHeight;
	lf.lfWeight = info.lfMenuFont.lfWeight;
	lf.lfItalic = info.lfMenuFont.lfItalic;

	m_fntPropList.CreateFontIndirect (&lf);

	m_wndPropList.SetFont (&m_fntPropList);
}
void CPropertyBar::InitPropList ()
{
	SetPropListFont ();

	m_wndPropList.EnableHeaderCtrl (FALSE);
	m_wndPropList.EnableDesciptionArea ();
	m_wndPropList.SetVSDotNetLook ();
	m_wndPropList.MarkModifiedProperties ();

	CBCGPProp* pGroup1 = new CBCGPProp (_T("Appearance"));

	pGroup1->AddSubItem (new CBCGPProp (_T("3D Look"), (_variant_t) false,
		_T("Specifies the dialog's font will be nonbold and controls will have a 3D border")));

	CBCGPProp* pProp = new CBCGPProp (_T("Border"), _T("Dialog Frame"),
		_T("One of: None, Thin, Resizable, or Dialog Frame"));
	pProp->AddOption (_T("None"));
	pProp->AddOption (_T("Thin"));
	pProp->AddOption (_T("Resizable"));
	pProp->AddOption (_T("Dialog Frame"));
	pProp->AllowEdit (FALSE);

	pGroup1->AddSubItem (pProp);
	pGroup1->AddSubItem (new CBCGPProp (_T("Caption"), (_variant_t) _T("About BCGProTest"),
		_T("Specifies the text that will be displayed in the dialog's title bar")));

	m_wndPropList.AddProperty (pGroup1);

	CBCGPProp* pSize = new CBCGPProp (_T("Window Size"), 0, TRUE);

	pProp = new CBCGPProp (_T("Height"), (_variant_t) 250l,
		_T("Specifies the dialog's height"));
	pProp->EnableSpinControl (TRUE, 0, 1000);
	pSize->AddSubItem (pProp);

	pProp = new CBCGPProp (	_T("Width"), (_variant_t) 150l,
		_T("Specifies the dialog's width"));
	pProp->EnableSpinControl ();
	pSize->AddSubItem (pProp);

	m_wndPropList.AddProperty (pSize);

	CBCGPProp* pGroup2 = new CBCGPProp (_T("Font"));

	LOGFONT lf;
	CFont* font = CFont::FromHandle ((HFONT) GetStockObject (DEFAULT_GUI_FONT));
	font->GetLogFont (&lf);

	lstrcpy (lf.lfFaceName, _T("Arial"));

	pGroup2->AddSubItem (new CBCGPFontProp (_T("Font"), lf, CF_EFFECTS | CF_SCREENFONTS, _T("Specifies the default font for the dialog")));
	pGroup2->AddSubItem (new CBCGPProp (_T("Use System Font"), (_variant_t) true, _T("Specifies that the dialog uses MS Shell Dlg font")));

	m_wndPropList.AddProperty (pGroup2);

	CBCGPProp* pGroup3 = new CBCGPProp (_T("Misc"));
	pProp = new CBCGPProp (_T("(Name)"), _T("IDD_ABOUT_BOX (dialog)"));
	pProp->Enable (FALSE);
	pGroup3->AddSubItem (pProp);

	CBCGPColorProp* pColorProp = new CBCGPColorProp (_T("Window Color"), RGB (210, 192, 254), NULL, _T("Specifies the default dialog color"));
	pColorProp->EnableOtherButton (_T("Other..."));
	pColorProp->EnableAutomaticButton (_T("Default"), ::GetSysColor (COLOR_3DFACE));
	pGroup3->AddSubItem (pColorProp);

	static TCHAR BASED_CODE szFilter[] = _T("Icon Files (*.ico)|*.ico|All Files (*.*)|*.*||");
	pGroup3->AddSubItem (new CBCGPFileProp (_T("Icon"), TRUE, _T(""), _T("ico"), 0, szFilter, _T("Specifies the dialog icon")));

	pGroup3->AddSubItem (new CBCGPFileProp (_T("Folder"), _T("c:\\")));

	COleDateTime date = COleDateTime::GetCurrentTime ();
	pGroup3->AddSubItem (new CBCGPDateTimeProp (_T("Date"), date,
		_T("Set a date"), 0, CBCGPDateTimeCtrl::DTM_DATE));

	pGroup3->AddSubItem (new CBCGPDateTimeProp (_T("Time"), date,
		_T("Set a time"), 0, CBCGPDateTimeCtrl::DTM_TIME));

	m_wndPropList.AddProperty (pGroup3);

	CBCGPProp* pGroup4 = new CBCGPProp (_T("Hierarchy"));

	CBCGPProp* pGroup41 = new CBCGPProp (_T("First sub-level"));
	pGroup4->AddSubItem (pGroup41);

	CBCGPProp* pGroup411 = new CBCGPProp (_T("Second sub-level"));
	pGroup41->AddSubItem (pGroup411);

	pGroup411->AddSubItem (new CBCGPProp (_T("Item 1"), (_variant_t) _T("Value 1"),
		_T("This is a description")));
	pGroup411->AddSubItem (new CBCGPProp (_T("Item 2"), (_variant_t) _T("Value 2"),
		_T("This is a description")));
	pGroup411->AddSubItem (new CBCGPProp (_T("Item 3"), (_variant_t) _T("Value 3"),
		_T("This is a description")));

	pGroup4->Expand (FALSE);
	m_wndPropList.AddProperty (pGroup4);
}

LRESULT CPropertyBar::OnPropertiesChanged(WPARAM wp, LPARAM lp)
{
	//wp - Contains the control ID of the property list.
	//lp   - A pointer to property (CBCGPProp) that changed
	ASSERT(lp);
	CBCGPProp* pProp = (CBCGPProp*)lp;

	CView* pView = CMainFrame::GetActiveViewWnd();
	ASSERT(m_pProp);
	if(m_pProp && pView)
	{
		::SendMessage(pView->GetSafeHwnd(), 
			WM_GEOM_PROPACTION,
			GPA_UIVALUE_CHANGED,
			pProp->GetData());
	}


	return TRUE;
}

BOOL CPropertyBar::OnPropAction(WPARAM wp, LPARAM lp)
{
	switch(wp) {
	case GPA_NEW_PROP:
		{
			m_pProp = (CPropHolder*)lp;
			if(m_pProp)
			{
				m_wndPropList.RemoveAll();
				m_pProp->Initialize(&m_wndPropList);
			}
		}
		break;
	case GPA_CLEAR:
		{
			m_wndPropList.RemoveAll();
			m_wndPropList.RedrawWindow();
		}
		break;
	default:
		break;
	}

	return TRUE;
}